layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table','laydate'], function () {
    var form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        admin = layui.admin;

    //执行一个laydate实例
    laydate.render({
        elem: '#created_time' //指定元素
    });

    table.render({
        elem: '#apply_table'
        , url: '/apply/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'id', title: 'id', width: 100, "align": "center",hide: true}
            , {field: 'oemId', width: 100, title: 'OEMID', "align": "center",hide: true}
            , {field: 'userId', width: 200, title: 'userId', "align": "center",hide: true}
            , {field: 'userName', width: 200, title: '申请人', "align": "center"}
            , {field: 'wcount', width: 150, title: '微信设备申请数', "align": "center"}
            , {field: 'wactualCount', width: 180, title: '微信设备实际发货数', "align": "center"}
            , {field: 'acount', width: 150, title: '支付宝设备申请数', "align": "center"}
            , {field: 'aactualCount', width: 180, title: '支付宝设备实际发货数', "align": "center"}
            , {field: 'state', width: 150, title: '状态', "align": "center", templet: function (d) {
                    if (d.state == 0) {
                        return "正在处理";
                    } else if (d.state == 1) {
                        return "已通过";
                    } else{
                        return "未通过";
                    }
                }}
            , {field: 'remarks', width: 200, title: '备注', "align": "center"}
            , {field: 'createdTime', width: 200, title: '申请时间', "align": "center"}
            , {field: 'updatedTime', width: 200, title: '审批时间', "align": "center"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {}
        , id: 'apply_table'
        , page: true
    });

    //申请设备
    $(".applyAdd_btn").click(function () {
        admin.popup({
            title: '编辑'
            , shade: 0
            , anim: -1
            , offset: "20px"
            , area: ['70%', '500px']
            , id: 'layadmin-layer-skin-modify'
            , skin: 'layui-anim layui-anim-upbit'
            , content: $("#add_apply_win")
            , end: function () {
                table.reload('apply_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {

                    }
                });
            }
        });
    });

    $("#apply_search_btn").click(function () {
        table.reload('apply_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                state: $("#state").val(),
                createdTime: $("#created_time").val()
            }
        });
    });


    /**
     * 新增提交
     */
    form.on('submit(addApplyOk)', function (data) {
        if(data.field.wCount.length == 0 && data.field.aCount.length == 0){
            layer.msg('请填写申请设备数量!', {icon: 5});
            return false;
        }
        $.ajax({
            url: "/apply/addApplyInfo",
            data: data.field,
            type: "post",
            success: function (result) {
                if (result.code == 1) {
                    layer.msg("提交成功", {
                        offset: '32%'
                        , icon: 1
                        , time: 3000
                        , btn: '知道了'
                        , btnAlign: "c"
                        , shade: 0.4
                        , yes: function () {
                            table.reload('apply_table', {});
                            layer.closeAll();
                        }
                    })
                } else {
                    layer.msg(result.message, {
                        offset: '32%'
                        , icon: 2
                        , time: 3000
                    }, function () {
                        layer.closeAll();
                    })
                }
            }
        });
        return false;
    });
});

